package com.erp.framework.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.SimpleConstants;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class UserDao implements SimpleConstants{
    public boolean hasAccessRight(String companyCode, String userId, String moduleName, Connection conn) {
        // temporary
        // if(true) return true;

        boolean rslt = false;

        PreparedStatement pStmt = null;
        ResultSet rs = null;

        if (CommonUtil.isEmpty(moduleName)) {
            return true;
        }

        try {

            /**
             * String query = "SELECT COUNT(PAGE_URL) RIGHT_COUNT" +" FROM
             * USER0A A, T_ACCESSRIGHT B" +" WHERE A.SEL_MENU = B.MENU_GP" +"
             * AND A.COMPANY_CODE='"+companyCode+"'" +" AND
             * A.USER_ID='"+userId+"'" +" AND A.STATUS='1'" +" AND
             * B.PAGE_URL='"+moduleName+"'";
             */

            String query = "SELECT COUNT(B.MENU_ID) RIGHT_COUNT" + " FROM T_USER A, T_COMPANY_MENU_GROUP B"
                    + " WHERE A.COMPANY_CODE = B.COMPANY_CODE" + " AND A.ROLE_GROUP_ID = B.ROLE_GROUP_ID"
                    + " AND A.COMPANY_CODE=?" + " AND A.USER_CODE=?" + " AND B.MENU_ID=?" + " AND A.ACTIVATION='"
                    + ProjectConstants.GENERIC_ACTIVE + "' "
                    + " AND A.GROUP_LEVEL <= B.VIEW_LEVEL";

            pStmt = conn.prepareStatement(query);

            int i = 1;
            pStmt.setString(i++, companyCode);
            pStmt.setString(i++, userId);
            pStmt.setString(i++, moduleName);

            rs = pStmt.executeQuery();

            if (rs != null) {
                while (rs.next()) {
                    if (rs.getLong("RIGHT_COUNT") > 0) {
                        rslt = true;
                    }
                }
            }
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }

            try {
                if (pStmt != null)
                    pStmt.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }

        }
        return rslt;

        // return true;
    }

    public boolean hasReportRight(String companyCode, String userId, String report, Connection conn) {
        boolean rslt = false;

        PreparedStatement pStmt = null;
        ResultSet rs = null;

        try {

            String query = "SELECT COUNT(B.REPORT) REPORT_COUNT" + " FROM USER0A A, T_ACCESSREPORT B"
                    + " WHERE A.SEL_MENU = B.MENU_GP" + " AND A.COMPANY_CODE=?" + " AND A.USER_ID=?" + " AND A.STATUS='1'"
                    + " AND B.REPORT=?";

            pStmt = conn.prepareStatement(query);

            int i = 1;
            pStmt.setString(i++, companyCode);
            pStmt.setString(i++, userId);
            pStmt.setString(i++, report);

            rs = pStmt.executeQuery();

            if (rs != null) {
                while (rs.next()) {
                    if (rs.getLong("REPORT_COUNT") > 0) {
                        rslt = true;
                    }
                }
            }
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }

            try {
                if (pStmt != null)
                    pStmt.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }

        }
        return rslt;
    }
}
